package com.hsnn.medstgmini.subscribe.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.std.model.StdHealthbureau;
import com.hsnn.medstgmini.base.std.service.StdHealthbureauManager;
import com.hsnn.medstgmini.pro.project.model.BidProject;
import com.hsnn.medstgmini.pro.project.service.BidProjectManager;
import com.hsnn.medstgmini.subscribe.model.BidSubscribe;
import com.hsnn.medstgmini.subscribe.model.BidSubscribeDetail;
import com.hsnn.medstgmini.subscribe.service.BidSubscribeManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.FormatDate;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 企业预约 控制器
 * @author 韩守松
 * @date 2015年8月27日
 */
@Controller
@RequestMapping("/bidSubscribe")
public class BidSubscribeController {
	private static final Logger log = Logger.getLogger(BidSubscribeController.class);
	
	@Autowired
	private BidSubscribeManager bidSubscribeManager;
	
	@Autowired
	private StdHealthbureauManager stdHealthbureauManager;
	
	@Autowired
	private BidProjectManager bidProjectManager;  
	
	@Autowired
	private HttpServletRequest request;
	
	/**
	 * @category 跳转到中心端项目预约页面
	 * @author 韩守松
	 * @date   2015年9月6日
	 * @param  @return
	 */
	@RequestMapping("/toSetSubProjListJsp")
	public String toSetSubProjListJsp(Model model){
		log.info("enter toProjectList method!");
		BidProject peoject = new BidProject();
		BidSubscribe bidSubscribe  = new BidSubscribe(); 
		try{
		SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		String defalutProjId=  user.getDefaultProjId();
		if ( StringUtils.isNotBlank(defalutProjId) ) {
			 model.addAttribute("defalutProjId",defalutProjId); 
			 peoject = bidProjectManager.getBidProjectAndTimeById(defalutProjId);
			 bidSubscribe=bidSubscribeManager.getIsSubscribe(defalutProjId);
			 if(null != bidSubscribe){
				 String isSet = bidSubscribe.getIsSet();
				 String bidSubscribeId = bidSubscribe.getBidSubscribeId();
				 Integer isEnable = bidSubscribe.getIsEnable();
				 if(!isSet.equals("0") && 0 != isEnable && StringUtils.isNotBlank(defalutProjId)){//跳转到详情页面
					 model.addAttribute("bidSubscribeId",bidSubscribeId);
					 model.addAttribute("isEnable",isEnable);
					 return "redirect:/bidSubscribe/toSubscribeDetailJsp.html";
				 }
				 if(!isSet.equals("0") && 0 == isEnable && StringUtils.isNotBlank(defalutProjId)){//跳转到编辑页面
					 model.addAttribute("bidSubscribeId",bidSubscribeId);
					 model.addAttribute("isEnable",isEnable);
					 return "redirect:/bidSubscribe/toSubscribeEditJsp.html";
				 }
				 if(isSet.equals("0") && StringUtils.isNotBlank(defalutProjId)){//跳转到新增页面
					 model.addAttribute("projId",defalutProjId);
					 model.addAttribute("defalutProjId",defalutProjId); 
					 model.addAttribute("projName",java.net.URLEncoder.encode(peoject.getName(),"UTF-8"));  
					 model.addAttribute("startTime",FormatDate.toShortFormat(peoject.getProjStartTime())); 
					 model.addAttribute("endTime",FormatDate.toShortFormat(peoject.getProjEndTime())); 
					 return "redirect:/bidSubscribe/toCenterSubscribeJsp.html";
				 }
				 
			 }
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "/subscribe/setSubProjList";
	}
	
	/**
	 * @category 获取中心端企业项目信息
	 * @author 韩守松
	 * @date   2015年9月6日
	 * @param  @param page
	 * @param  @param bidSubscribe
	 * @param  @return
	 */
	@RequestMapping("/getSetSubProjData")
	@ResponseBody
	public Pagination getSetSubProjData(Pagination page,BidProject bidProject){
		page = new Pagination(request);
		SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		String projId = user.getDefaultProjId();
		if(StringUtils.isNotBlank(projId)){
			bidProject.setProjId(projId);
		}
		page=bidSubscribeManager.getProjList(page, bidProject);
		return page;
	}
	
	/**
	 * @category 中心端预约
	 * @author 韩守松
	 * @date   2015年8月27日
	 * @param  @return
	 */
	@RequestMapping("/toCenterSubscribeJsp")
	public  String toCenterSubscribeJsp(String projId,String projName){
		log.info("enter toProjectList method!");
		try{
		StdHealthbureau stdHealthbureau = new StdHealthbureau();
		stdHealthbureau = stdHealthbureauManager.getStdHealthbureauData();
		request.setAttribute("address",stdHealthbureau.getHospAddress());
		request.setAttribute("startTime", request.getParameter("startTime"));
		request.setAttribute("endTime", request.getParameter("endTime"));
		request.setAttribute("projId", projId);
		request.setAttribute("defalutProjId",request.getParameter("defalutProjId"));
		request.setAttribute("projName", java.net.URLDecoder.decode(projName,"UTF-8"));
		}catch(Exception e){
			e.printStackTrace();
		}
		return "/subscribe/setSubscribeAdd";
	}

	/**
	 * @category 保存预约信息
	 * @author 韩守松
	 * @date   2015年8月28日
	 * @param  @param page
	 * @param  @param bidSubscribe
	 * @param  @param bidSubscribeDetail
	 * @param  @return
	 */
	@RequestMapping("/saveBidSubscribe")
	@ResponseBody
	public Pagination saveBidSubscribe(Pagination page,BidSubscribe bidSubscribe,String defalutProjId){
		try{
			SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
			if(StringUtils.isNotBlank(defalutProjId)){
				bidSubscribe.setProjId(defalutProjId);
			}
			bidSubscribe.setUpdUser(user.getName());
			bidSubscribe.setUpdDatetime(new Date());
			bidSubscribeManager.save(bidSubscribe);
			page.setSuccess(true);
			if(StringUtils.isNotBlank(defalutProjId)){
				page.getConditions().put("defalutProjId", bidSubscribe.getProjId());
				page.getConditions().put("bidSubscribeId", bidSubscribe.getBidSubscribeId());
				page.getConditions().put("isEnable", bidSubscribe.getIsEnable().toString());
			}
		}catch(Exception e){
			log.error("保存预约信息失败"+e);
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * @category 跳转到预约设置列表页面
	 * @author 韩守松
	 * @date   2015年8月29日
	 * @param  @return
	 */
	@RequestMapping("/toBidSubscribeListJsp")
	public String toBidSubscribeListJsp(){
		String projId = request.getParameter("projId");
		request.setAttribute("projId", projId);
		return "/subscribe/setSubscribeList";
	}
	
	/**
	 * @category 获取预约设置信息
	 * @author 韩守松
	 * @date   2015年8月29日
	 * @param  @param page
	 * @param  @param bidSubscribe
	 * @param  @return
	 */
	@RequestMapping("/getBidSubscribeData")
	@ResponseBody
	public Pagination getBidSubscribeData(Pagination page,BidSubscribe bidSubscribe){
		page = new Pagination(request);
		page=bidSubscribeManager.getList(page, bidSubscribe);
		return page;
	}
	
	/**
	 * @category 跳转到预约修改页面
	 * @author 韩守松
	 * @date   2015年8月29日
	 * @param  @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/toSubscribeEditJsp")
	public String toSubscribeEditJsp(BidSubscribe bidSubscribe,String defalutProjId){
		List list = bidSubscribeManager.getBidSubscribeById(bidSubscribe.getBidSubscribeId());
		request.setAttribute("list", list.get(0));
		request.setAttribute("defalutProjId", defalutProjId);
		return "/subscribe/setSubscribeEdit";
	}
	
	/**
	 * @category 预约设置修改
	 * @author 韩守松
	 * @date   2015年8月30日
	 * @param  @param page
	 * @param  @param bidSubscribe
	 * @param  @return
	 */
	@RequestMapping("/updateSubscribe")
	@ResponseBody
	public Pagination updateSubscribe(Pagination page, BidSubscribe bidSubscribe){
		try {
			SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
			String defalutProjId = user.getDefaultProjId();
			bidSubscribe.setUpdUser(user.getName());
			bidSubscribe.setUpdDatetime(new Date());
			page=bidSubscribeManager.update(bidSubscribe);
			page.setSuccess(true);
			if(StringUtils.isNotBlank(defalutProjId)){
				page.getConditions().put("defalutProjId", bidSubscribe.getProjId());
				page.getConditions().put("bidSubscribeId", bidSubscribe.getBidSubscribeId());
				page.getConditions().put("isEnable", bidSubscribe.getIsEnable().toString());
			}
		} catch (Exception e) {
			log.error("预约信息修改失败"+e);
			page.setSuccess(false);
		}
		return page;
	}
	@RequestMapping("/toSubscribeStatusUpdate")
	@ResponseBody
	public Pagination toSubscribeStatusUpdate(Pagination page){
		String arrayStr = request.getParameter("bidSubscribeId");
		String isEnable = request.getParameter("isEnable");
		
		String bidSubscribeId ="";
		try {
			String[] ids = arrayStr.substring(0, arrayStr.length()-1).split(",");
			BidSubscribe bidSubscribe = new BidSubscribe();
			SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
			bidSubscribe.setUpdUser(user.getId());
			bidSubscribe.setUpdDatetime(new Date());
			for(int i=0;i<ids.length;i++){
				bidSubscribeId = ids[i];
				if(StringUtils.isNotBlank(bidSubscribeId)){
					bidSubscribe.setBidSubscribeId(bidSubscribeId);
				}
				if(StringUtils.isNotBlank(isEnable)){
					if("1".equals(isEnable)){
						bidSubscribe.setIsEnable(Integer.valueOf(isEnable));
						page=bidSubscribeManager.statusUpdate(bidSubscribe);
						page.setSuccess(true);
					}
					if("2".equals(isEnable)){
						bidSubscribe.setIsEnable(Integer.valueOf(isEnable));
						page=bidSubscribeManager.statusUpdate(bidSubscribe);
						page.setSuccess(true);
					}
					
				}
				
			}
			
		} catch (Exception e) {
			log.error("预约信息修改失败"+e);
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * @category 生产企业端预约
	 * @author 韩守松
	 * @date   2015年8月27日
	 * @param  @return
	 */
	@RequestMapping("/toProduceSubscribeJsp")
	public  String toProduceSubscribeJsp(Model model){
		BidProject peoject = new BidProject();
		BidSubscribe bidSubscribe  = new BidSubscribe(); 
		try{
		SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		String defalutProjId=  user.getDefaultProjId();
		if ( StringUtils.isNotBlank(defalutProjId) ) {
			 model.addAttribute("defalutProjId",defalutProjId); 
			 peoject = bidProjectManager.getBidProjectById(defalutProjId);
			 bidSubscribe=bidSubscribeManager.getIsSubscribe(defalutProjId);
			 if(null != bidSubscribe){
				 String bidSubscribeId = bidSubscribe.getBidSubscribeId();
			     model.addAttribute("defalutProjId",defalutProjId); 
			     model.addAttribute("bidSubscribeId",bidSubscribeId);
			     model.addAttribute("projName",java.net.URLEncoder.encode(peoject.getName(),"UTF-8"));  
			     return "redirect:/bidSubscribeDetail/toProduceSubscribeAddJsp.html";
			 }
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "/subscribe/produce/lookList";
	}
	
	@RequestMapping("/toSubscribeCompListJsp")
	public String toSubscribeCompListJsp(){
		String number = request.getParameter("number");
		String bidSubscribeId = request.getParameter("bidSubscribeId");
		String projId =  request.getParameter("projId");
		request.setAttribute("bidSubscribeId", bidSubscribeId);
		request.setAttribute("number", number);
		request.setAttribute("projId", projId);
		return "/subscribe/subscribeCompList";
	}
	/**
	 * @category 获取已预约企业信息
	 * @author 韩守松
	 * @date   2015年9月7日
	 * @param  @return
	 */
	@RequestMapping("/getSubscribeCompListData")
	@ResponseBody
	public Pagination getSubscribeCompListData(){
		Pagination page = new Pagination(request);
		try{
		String number = request.getParameter("number");
		String bidSubscribeId = request.getParameter("bidSubscribeId");
		String subscribeDate = request.getParameter("subscribeDate");
		BidSubscribeDetail bidSubscribeDetail = new BidSubscribeDetail();
		bidSubscribeDetail.setBidSubscribeId(bidSubscribeId);
		if(StringUtils.isNotBlank(subscribeDate)){
			bidSubscribeDetail.setSubscribeDate(new SimpleDateFormat("yyyy-MM-dd").parse(subscribeDate));
			
		}
		if("1".equals(number)){
			page = bidSubscribeManager.getSubCompList(bidSubscribeDetail);
		}
		if("2".equals(number)){
			page = bidSubscribeManager.getSubCompTimeTwoList(bidSubscribeDetail);
		}
		}catch(Exception e){
			log.error("获取预约企业错误");
		}
		return page;
	}
	
	/**
	 * @category 跳转到预约详情页面
	 * @author 韩守松
	 * @date   2015年8月29日
	 * @param  @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/toSubscribeDetailJsp")
	public String toSubscribeDetailJsp(BidSubscribe bidSubscribe){
		List list = bidSubscribeManager.getBidSubscribeById(bidSubscribe.getBidSubscribeId());
		request.setAttribute("list", list.get(0));
		request.setAttribute("isEnable", request.getParameter("isEnable"));
		request.setAttribute("defalutProjId",request.getParameter("defalutProjId"));
		return "/subscribe/setSubscribeDetail";
	}
	
	/**
	 * @category 跳转到查看预约情况页面
	 * @author 韩守松
	 * @date   2015年9月17日
	 * @param  @return
	 */
	@RequestMapping("/toCompRecordJsp")
	public String toCompRecordJsp(String projId,String bidSubscribeId,String defalutProjId){
		request.setAttribute("defalutProjId", defalutProjId);
		request.setAttribute("projId", projId);
		request.setAttribute("bidSubscribeId", bidSubscribeId);
		request.setAttribute("projName", request.getParameter("projName"));
		return "/subscribe/setCompRecordList";
	}
	
	/**
	 * @category 查看预约情况
	 * @author 韩守松
	 * @date   2015年9月17日
	 * @param  @param bidSubscribeDetail
	 * @param  @return
	 */
	@RequestMapping("/getCompRecord")
	@ResponseBody
	public Pagination getCompRecord(){
		Pagination page = new Pagination(request);
		try{
		String projId = request.getParameter("projId");
		String subscribeDate = request.getParameter("subscribeDate");
		BidSubscribeDetail bidSubscribeDetail = new BidSubscribeDetail();
		bidSubscribeDetail.setProjId(projId);
		if(StringUtils.isNotBlank(subscribeDate)){
			bidSubscribeDetail.setSubscribeDate(new SimpleDateFormat("yyyy-MM-dd").parse(subscribeDate));
		}
			page = bidSubscribeManager.getCompRecord(page, bidSubscribeDetail);
		}catch(Exception e){
			log.error("获取预约企业错误");
		}
		return page;
	}
	/**
	 * @category 开启预约规则
	 * @author 韩守松
	 * @date   2015年9月17日
	 * @param  @param page
	 * @param  @param bidSubscribeId
	 * @param  @return
	 */
	@RequestMapping("/startIsEnable")
	@ResponseBody
	public Pagination startIsEnable(Pagination page,String bidSubscribeId,String isEnable){
		BidSubscribe bidSubscribe = new BidSubscribe();
		try {
			SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
			bidSubscribe.setUpdUser(user.getId());
			bidSubscribe.setUpdDatetime(new Date());
			bidSubscribe.setBidSubscribeId(bidSubscribeId);
			if(StringUtils.isNotBlank(isEnable)){
				if("1".equals(isEnable)){
					bidSubscribe.setIsEnable(Integer.valueOf(isEnable));
					page=bidSubscribeManager.isEnableUpdate(bidSubscribe);
				}
			}
		} catch (Exception e) {
			log.error("更新预约规则信息失败"+e);
			page.setSuccess(false);
		}
		return page;
	}
	
}
